package com.hello.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hello.util.HtmlToWord;
import com.hello.util.OfficeUtil;


@Controller
public class FileToAction {
	
	private static final Logger log= LoggerFactory.getLogger(FileToAction.class);
	
	private String data;
	private String charts;

	@GetMapping("/home")
	public String home(Model model) {
		Map<String, Object> map = new HashMap<>();
		map.put("name", "cyx");
		map.put("age", 21);
		log.error("出現錯誤");
		model.addAttribute("name", map);
		return "index";
	}
	
	/**
	 * 上传文件解析
	* @Title: fileTo 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param file    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@PostMapping("/fileto")
	public void fileTo(@RequestParam("file")MultipartFile file,HttpServletRequest request) {
		String fileName = file.getOriginalFilename();
		String ftype = fileName.substring(fileName.lastIndexOf(".")+1);
		try {
			//获取文件流
			FileInputStream fis = (FileInputStream) file.getInputStream();
			//项目根目录(也是存html的路径)
			String jurl = request.getSession().getServletContext().getRealPath("/");
			switch (ftype) {
			case "docx":
				OfficeUtil.Office offile = OfficeUtil.docxToHtml2(fis,jurl);
				break;
			case "doc":
				OfficeUtil.Office office = OfficeUtil.docToHTml(fis, jurl);
				break;

			default:
				break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	* @Title: wordTo 
	* @Description: TODO(将html转成word) 
	* @param @param data
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@PostMapping("/up")
	@ResponseBody
	public void wordTo1(@RequestParam(value="data")String data,@RequestParam("charts")String charts){
		System.out.println(data);
		this.setData(data);
		this.setCharts(charts);
	}
	
	@GetMapping("/toWhile")
	@ResponseBody
	public void toWhile(){
		while (true) {
			try {
				Thread.sleep(1000);
				System.out.println(new Date());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@GetMapping("/to")
	public ResponseEntity<byte[]> wordTo(HttpServletRequest request){
		String jurl = request.getSession().getServletContext().getRealPath("/");
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", "hello.docx");
		return new ResponseEntity<>(HtmlToWord.resolveHtml(this.getData(), jurl),headers, HttpStatus.OK);
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getCharts() {
		return charts;
	}

	public void setCharts(String charts) {
		this.charts = charts;
	}
}
